//
//  MyYahooWeather.m
//  Ghac 4S
//
//  Created by apple on 11-12-8.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyYahooWeather.h"
#import <MapKit/MapKit.h>
#import "KURL.h"
#import "KXML.h"
#import "KXMLNode.h"

@interface MyYahooWeather(private) 
-(void)askCityWeatherWithWoeid:(NSString *)woeid;
-(void)updateLocationComplete:(id)sender;
-(void)updateWeatherComplete:(id)sender;
@end

@implementation MyYahooWeather
-(id)initWithDelegate:(id<WeatherDelegate>)delegate
{
    if ((self = [super initWithDelegate:delegate]))
    {
    }
    return self;
}
-(void)updateLocationCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSString* str = [NSString stringWithFormat:@"%@q=%f,%f&gflags=XR&locale=zh-cn", WEATHER_CITY_QUERYPATH, coordinate.latitude, coordinate.longitude];
    NSURL* cityinfourl = [NSURL URLWithString:str];
    KURLLoader* loader = [[KURLLoader alloc] initWithDelegate:self];
    KURLRequest* req = [[KURLRequest alloc] initWithURL:cityinfourl];
    [loader load:req];
    [loader addEventListener:KLOADER_COMPLETE sector:@selector(updateLocationComplete:)];
    [req release];
    
    
}
-(void)updateLocationComplete:(id)sender
{
    KURLLoader* loader = sender;
    NSStringEncoding encode = NSUTF8StringEncoding;
    NSString* xmlStr = [[NSString alloc] initWithData:loader.data encoding:encode];
    if (xmlStr == nil)
    {
        return;
    }
    
    KXML* cityinfo = [[KXML alloc] initWithString:xmlStr];
    [xmlStr release];
    [loader release];
    
    if ([[cityinfo valueForKeyPath:@"Error.__nodeValue__"] isEqualToString:@"0"])
    {
        [self askCityWeatherWithWoeid:[cityinfo valueForKeyPath:@"Result.woeid.__nodeValue__"]];
    }
    else
    {
        NSError* err = [NSError errorWithDomain:@"error" code:0 userInfo:[NSDictionary dictionaryWithObject:[cityinfo valueForKeyPath:@"ErrorMessage.__nodeValue__"] forKey:@"ErrorMessage"]];
        [self errorOnGettingCity:err];
    }
    [cityinfo release];
}
-(void)askCityWeatherWithWoeid:(NSString *)woeid
{
    NSString* str = [NSString stringWithFormat:@"%@w=%@&u=c&l=zh-cn", WEATHER_QUERYPATH, woeid];
    NSURL* weatherinfourl = [NSURL URLWithString:str];
    
    KURLLoader* loader = [[KURLLoader alloc] initWithDelegate:self];
    KURLRequest* req = [[KURLRequest alloc] initWithURL:weatherinfourl];
    [loader load:req];
    [loader addEventListener:KLOADER_COMPLETE sector:@selector(updateWeatherComplete:)];
    [req release];
    
    
    
    
}
-(void)updateWeatherComplete:(id)sender
{
    KURLLoader* loader = sender;
    NSStringEncoding encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString* xmlStr = [[NSString alloc] initWithData:loader.data encoding:encode];
    if (xmlStr == nil)
    {
        return;
    }
    KXML* weatherinfo = [[KXML alloc] initWithString:xmlStr];
    KXMLNode* conditionNode = [weatherinfo valueForKeyPath:@"channel.item.yweather:condition"];
    self.templeture = [conditionNode valueForKeyPath:@"@temp"];
    
    NSString* imageStr =[NSString stringWithFormat:@"%@%@.gif", WEATHER_IMAGEPATH, [conditionNode valueForKeyPath:@"@code"]];
    self.weatherImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]]];
    [m_delegate onWeatherUpdate:self];
    [weatherinfo release];
    [loader release];
    [xmlStr release];
}
@end
